package com.accesshollywood.grfxOrdering.panels;

import java.awt.Component;
import java.awt.Dimension;

import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.ListSelectionModel;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;

import org.jdesktop.swingx.VerticalLayout;

import com.accesshollywood.components.EditableListWithPopup;
import com.accesshollywood.components.HasPopupMenu;
import com.accesshollywood.grfxOrdering.GRFXOrdersMain;
import com.accesshollywood.grfxOrdering.OrderMainGUI;
import com.accesshollywood.indexables.ViewFilterPreset;
import com.accesshollywood.viewFiltering.ViewFilters;

public class ViewFilterPresetPanel extends JPanel implements HasPopupMenu {
	JScrollPane								scrollPane;
	EditableListWithPopup<ViewFilterPreset>	viewPresetList;
	// JButton btn_SavePreset;
	public ViewFiltersMasterPanel			viewFiltersMasterPanel;

	@SuppressWarnings("unchecked")
	public ViewFilterPresetPanel() {
		setLayout(new VerticalLayout());

		viewPresetList = new EditableListWithPopup<ViewFilterPreset>(ViewFilters.toEditableListModel(), 11, ViewFilterPreset.class);
		// viewPresetList.setModel(ViewFilters.toMutableListModel());
		// viewPresetList.setCellRenderer(new ViewPresetRenderer());
		viewPresetList.getSelectionModel().addListSelectionListener(new ViewPresetSelectionListener());

		scrollPane = new JScrollPane(viewPresetList);
		scrollPane.setAlignmentX(Component.LEFT_ALIGNMENT);
		scrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);

		add(scrollPane);

		// btn_SavePreset = new JButton("Save Preset...");
		// add(btn_SavePreset);
		refresh(true);

		setupPopup();
	}

	/*
	 * public class ViewPresetRenderer extends DefaultListCellRenderer {
	 * public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected, boolean hasFocus) {
	 * JLabel label = (JLabel) super.getListCellRendererComponent(list, value, index, isSelected, hasFocus);
	 * if (value instanceof Indexed) {
	 * ViewFilterPreset filterPreset = (ViewFilterPreset) value;
	 * label.setText(filterPreset.getName());
	 * } else {
	 * // Clear old icon; needed in 1st release of JDK 1.2
	 * label.setIcon(null);
	 * }
	 * return (label);
	 * }
	 * }
	 */
	public class ViewPresetSelectionListener implements ListSelectionListener {
		@Override
		public void valueChanged(ListSelectionEvent e) {
			if (!e.getValueIsAdjusting()) {
				ListSelectionModel lsm = (ListSelectionModel) e.getSource();

				int firstIndex = e.getFirstIndex();
				int lastIndex = e.getLastIndex();

				if (!lsm.isSelectionEmpty()) {
					// Find out which indexes are selected.
					int minIndex = lsm.getMinSelectionIndex();
					int maxIndex = lsm.getMaxSelectionIndex();
					for (int i = minIndex; i <= maxIndex; i++) {
						if (lsm.isSelectedIndex(i)) {
							ViewFilterPreset viewFilterPreset = (ViewFilterPreset) viewPresetList.getModel().getElementAt(i);
							viewFiltersMasterPanel.setFromPreset(viewFilterPreset);
						}
					}
				}

				for (int i = 0; i < OrderMainGUI.allFoldables.size(); i++) {
					if (OrderMainGUI.allFoldables.get(i) instanceof ViewFilterGroupPanel) {
						((ViewFilterGroupPanel) OrderMainGUI.allFoldables.get(i)).setBackgroundIfAnyChecked();
					}
				}
				// ((OrderListPanel) viewFiltersMasterPanel.parent).refreshOrderGUI(true);
				viewPresetList.clearSelection();
			}
		}
	}

	public void refresh(boolean fromSQL) {
		// viewPresetList.setModel(ViewFilters.toPresetListModel());
		// viewPresetList.refresh(fromSQL);
		// int h = Math.min(7, viewPresetList.getModel().getSize()) * 17;
		scrollPane.setPreferredSize(new Dimension(scrollPane.getPreferredSize().width, 5 * 17));
		if (GRFXOrdersMain.gui != null) {
			GRFXOrdersMain.gui.repaint();
		}
		revalidate();
		repaint();
	}

	@Override
	public void setupPopup() {
		// viewPresetList.popup.addMenuItem("Add", new AddActionListener());
		// viewPresetList.popup.addMenuItem("Rename", new EditActionListener());
		// viewPresetList.popup.addMenuItem("Delete", new DeleteActionListener());
	}
	/*
	 * public class AddActionListener implements ActionListener {
	 * @Override
	 * public void actionPerformed(ActionEvent arg0) {
	 * // int id = GFXSQL.addString("viewPreset", "name", "New Preset...");
	 * ViewFilterPreset newPreset = new ViewFilterPreset();
	 * if (newPreset.getName() != null && newPreset.getName().length() > 0) {
	 * Collections.sort(ViewFiltersMasterPanel.selectedFilters, new Comparator() {
	 * public int compare(Object o1, Object o2) {
	 * ViewFilter v1 = (ViewFilter) o1;
	 * ViewFilter v2 = (ViewFilter) o2;
	 * return v1.getGroupName().compareToIgnoreCase(v2.getGroupName());
	 * }
	 * });
	 * if (ViewFiltersMasterPanel.selectedFilters.size() > 0) {
	 * for (int i = 0; i < ViewFiltersMasterPanel.selectedFilters.size(); i++) {
	 * ViewFilter currFilter = (ViewFilter) ViewFiltersMasterPanel.selectedFilters.get(i);
	 * //newPreset.checkedFilters.put(currFilter.hashCode(), currFilter);
	 * newPreset.getViewFilterList().add(currFilter);
	 * }
	 * }
	 * newPreset.toSQL();
	 * ((EditableListModel)viewPresetList.getModel()).addElement(newPreset);
	 * //((DefaultListModel) viewPresetList.getModel()).addElement(indexedViewFilterPreset);
	 * }
	 * }
	 * }
	 */

	/*
	 * public class EditActionListener implements ActionListener {
	 * @Override
	 * public void actionPerformed(ActionEvent e) {
	 * Point loc = new Point(viewPresetList.popup.X,viewPresetList.popup.Y);
	 * int row = viewPresetList.locationToIndex(loc);
	 * // viewPresetList.beginEdit(row);
	 * }
	 * }
	 * public class DeleteActionListener implements ActionListener {
	 * @Override
	 * public void actionPerformed(ActionEvent e) {
	 * Point loc = new Point(viewPresetList.popup.X,viewPresetList.popup.Y);
	 * int row = viewPresetList.locationToIndex(loc);
	 * // IndexedSQL<ViewFilterPreset> currIndexedViewPreset = (IndexedSQL<ViewFilterPreset>)viewPresetList.getValueAt(row);
	 * // GFXSQL.removeViewPreset(currIndexedViewPreset);
	 * // ((DefaultListModel) viewPresetList.getModel()).removeElement(currIndexedViewPreset);
	 * }
	 * }
	 */
}
